﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LuxDiner.Model.Orders;
namespace LuxDiner.Command.Orders
{
    public class WarehouseCommand
    {
        public static WarehouseModel GetModel(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.warehouses.FirstOrDefault(p => p.ID == id);
                    if (d != null)
                    {
                        var model = Convertor.ConvertTo<WarehouseModel, warehouse>(d);
                        return model;
                    }
                }
            }
            return null;
        }

        public static WarehouseModel CreateNew(WarehouseModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    CheckSave(model, db);
                    var d = Convertor.ConvertFrom<warehouse, WarehouseModel>(model);
                    db.DataContext.warehouses.Add(d);
                    db.DataContext.SaveChanges();
                    model.ID = d.ID;
                    return model;
                }
            }
            return null;
        }

        public static void Update(WarehouseModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.warehouses.FirstOrDefault(p => p.ID == model.ID);
                    if (d == null)
                        throw new ApplicationException("仓库编号无效");
                    CheckSave(model, db);
                    Convertor.UpdateValue(model, d);
                    db.DataContext.SaveChanges();
                }
            }
        }

        private static void CheckSave(WarehouseModel model, DBCentre db)
        {
            if (string.IsNullOrEmpty(model.House))
                throw new ApplicationException("请提供仓库名称");
            if (db != null)
            {
                if (db.DataContext.warehouses.Any(p=>p.House==model.House && p.ID!=model.ID))
                    throw new ApplicationException("仓库名称重复");
            }
        }

        public static WarehouseCollection GetAll()
        {
            WarehouseCollection result=new WarehouseCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<warehouse> q = db.DataContext.warehouses;
                var n = q.Count();
                if (n > 0)
                {
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<WarehouseModel, warehouse, WarehouseCollection>(lst);
                    result.TotalRecordsCount = n;
                }
            }
            return result;
        }
    }
}
